﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using APdal;

namespace Operacional
{
    public partial class frmEntrada : Form
    {
        #region Declarações

        public static SqlConnection conn;
        public static DataSet dsFilkemp = new DataSet("dsFilkemp");
        private SqlDataAdapter _daUsers;
        public static SqlDataAdapter daLinhasTarefas, daTarefas;
        public static DataView dvUsers;
        private static SqlCommandBuilder _builder;
        private frmLinhas _linhas;
        private readonly frmLogin _login = new frmLogin();              
        public static Timer tmrClock, tmrLogin;
        private dataLink _myDal = new dataLink();

        #endregion

        public frmEntrada()
        {
            InitializeComponent();
            Application.Idle += new EventHandler(Application_Idle);        
        }

        private void fmrEntrada_Load(object sender, EventArgs e)
        {
            conn = dataLink.conn;            

            Size = Screen.PrimaryScreen.WorkingArea.Size;
            MinimumSize = Size;
            Location = new System.Drawing.Point(0, 0);
            Show();

            #region Timers
            tmrLogin = new Timer { Enabled = true, Interval = 180000 };
            tmrLogin.Tick += new EventHandler(tmrLogin_Tick);
            tmrClock = new Timer { Enabled = true, Interval = 100 };
            tmrClock.Tick += new EventHandler(tmrClock_Tick);
            #endregion

            lblClock.BackColor = toolStrip.BackColor;
            pictureBox1.BackColor = toolStrip.BackColor;

            try
            {
                #region Tables
                dsFilkemp.Tables.Add("dtUsers");
                dsFilkemp.Tables.Add("dtLinhas_Tarefas");
                dsFilkemp.Tables.Add("dtTarefas");
                dsFilkemp.Tables["dtUsers"].CaseSensitive = true;
                dsFilkemp.Tables.Add("dtLinhas");
                #endregion

                #region DataAdapters
                _daUsers = new SqlDataAdapter(@"Select * from Utilizadores", conn);
                daLinhasTarefas = new SqlDataAdapter(@"Select * from Linhas_Tarefas", conn);
                daTarefas = new SqlDataAdapter(@"Select * from Tarefas", conn);
                #endregion

                _builder = new SqlCommandBuilder(daLinhasTarefas);
                daLinhasTarefas.InsertCommand = _builder.GetInsertCommand();

                _daUsers.Fill(dsFilkemp, "dtUsers");
                dvUsers = new DataView(dsFilkemp.Tables["dtUsers"]);

                _login.ShowDialog(this);
                _linhas = new frmLinhas { MdiParent = this };
                _linhas.Show();
            }
            catch
            {                
                FormClosing -= frmEntrada_FormClosing;
                Application.Exit();
            }
        }

        private void tmrClock_Tick(object sender, EventArgs e)
        {
            lblClock.Text = DateTime.Now.ToString("HH:mm");
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Close();
            Application.Exit();
        }

        private void btnLinhas_Click(object sender, EventArgs e)
        {
            bool isOpened = false;
            foreach (Form f in Application.OpenForms)
                isOpened = f.Name == "frmLinhas";

            if (isOpened == false)
            {
                closeAllForms(Application.OpenForms);
                _linhas = new frmLinhas { MdiParent = this };
                _linhas.Show();
            }
        }

        public void closeAllForms(FormCollection Forms)
        { for (int i = 0; i < Forms.Count; i++)  if (Forms[i].Name != "frmEntrada") Forms[i].Close(); }

        private void LockTool_Click(object sender, EventArgs e)
        {
            LockTool.Image = Properties.Resources.LOCK;

            frmLogin Login = new frmLogin();
            Login.ShowDialog();
        }

        private void KeyboardTool_Click(object sender, EventArgs e)
        {
            try { System.Diagnostics.Process.Start("OSK.exe"); }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        public static void Application_Idle(Object sender, EventArgs e)
        {
            tmrLogin.Enabled = true;
        }

        private void tmrLogin_Tick(object sender, EventArgs e)
        {
            bool isOpened = false;
            foreach (Form f in Application.OpenForms) if (f.Name == "frmLogin") isOpened = true;
            if (!isOpened)
            {
                frmLogin login = new frmLogin();
                login.ShowDialog();
            }
        }

        private void frmEntrada_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Deseja mesmo sair?", "Confirme saída", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                e.Cancel = true;
        }

        public void lblStatusText(string s)
        {
            lblStatus.Text = s;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        { System.Diagnostics.Process.Start("http://www.anaprog.net"); }
    }
}